package com.youlemall.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youlemall.common.pojo.PageResult;
import com.youlemall.mapper.SpecificationOptionMapper;
import com.youlemall.mapper.TypeTemplateMapper;
import com.youlemall.pojo.SpecificationOption;
import com.youlemall.pojo.TypeTemplate;
import com.youlemall.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 模板类实现接口
 * 2 * @Author: kevin
 * 3 * @Date: 2019/7/15 16:37
 * 4
 */

@Service
@Transactional
public class TypeTemplateServiceImpl implements TypeTemplateService {
	@Autowired
	private TypeTemplateMapper typeTemplateMapper;

	@Autowired
	private SpecificationOptionMapper som;



	@Override
	public void save(TypeTemplate typeTemplate) {
		try {
			typeTemplateMapper.insertSelective(typeTemplate);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(TypeTemplate typeTemplate) {
		try {
			int i = typeTemplateMapper.updateByPrimaryKeySelective(typeTemplate);
			System.out.println(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void delete(Serializable id) {

	}

	@Override
	public void deleteAll(Serializable[] ids) {
		try {
			Example example =new Example(TypeTemplate.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andIn("id", Arrays.asList(ids));
			typeTemplateMapper.deleteByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public TypeTemplate findOne(Serializable id) {
		return typeTemplateMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TypeTemplate> findAll() {
		return null;
	}

	@Override
	public PageResult findByPage(TypeTemplate typeTemplate, int page, int rows) {
		try {
			PageInfo<TypeTemplate> pageInfo = PageHelper.startPage(page, rows).doSelectPageInfo(new ISelect() {
				@Override
				public void doSelect() {
					typeTemplateMapper.selectByname(typeTemplate);
				}
			});
			return new PageResult(pageInfo.getPages(), pageInfo.getList());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Map<String, Object>> findtypeTemplate() {
		try {
			return typeTemplateMapper.findtypeTemplate();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Map> findSpecByTemplateId(Long id) {
		try {
			TypeTemplate typeTemplate = typeTemplateMapper.selectByPrimaryKey(id);
			List<Map> maps = JSON.parseArray(typeTemplate.getSpecIds(), Map.class);
			for (Map map : maps) {
				SpecificationOption specificationOption =new SpecificationOption();
				specificationOption.setSpecId(Long.valueOf(map.get("id").toString()));
				List<SpecificationOption> specOptions = som.select(specificationOption);
				map.put("Options",specOptions);
			}
			return maps;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
